/* 模板管理
 * @Author: wujie 
 * @Date: 2018-06-12 11:06:22 
 * @Last Modified by: wujie
 * @Last Modified time: 2018-07-04 10:48:06
 */
import React,{ Component } from 'react'
import { Row, Modal } from 'antd'
import components from '@/components/index'
import { connect } from 'react-redux';
import actions from '@/actions/template'
import { setData } from '@/actions/templateEdit'
import { getTemplateStatusEnum } from '@/actions/enum'
import util from '@/utils'

const { TemplateSearch, TemplateTable, Card } = components

const { confirm } = Modal

class Template extends Component {

  render(){

    const searchProps = util.pick(this.props, [
      'template',
      'status',
      'templateStatusEnum',
      'startTime',
      'endTime',
    ])

    const searchFunProps = util.pick(this, [
      'onChangeTemplate',
      'onChangeStatus',
      'onChangeStartTime',
      'onChangeEndTime',
      'onSearch',
      'onAdd',
    ])

    const tableProps = util.pick(this.props, [
      'data',
      'pageNum',
      'pageSize',
      'total',
      'loading',
      'templateStatusEnum',
    ])

    const tableFunProps = util.pick(this, [
      'onDelete',
      'onChangePagination',
    ])

    return (
      <div>
        <Card title="模板管理">
          <Row style={{padding:24,backgroundColor: '#fff'}}>
            <TemplateSearch  {...searchProps} {...searchFunProps} />
            <TemplateTable {...tableProps} {...tableFunProps} />
          </Row>
        </Card>
      </div>
    )
  }

  componentDidMount() {
    this.props.getTemplateStatusEnum()
    this.props.getData()
  }

  onChangeTemplate = ({ target: { value } }) => {
    this.props.setTemplate(value)
  }

  onChangeStatus = (value) => {
    this.props.setStatus(value)
  }

  onChangeStartTime = (value) => {
    this.props.setStartTime(value)
  }

  onChangeEndTime = (value) => {
    this.props.setEndTime(value)
  }

  onSearch = () => {
    this.props.setPageNum(1)
    this.props.getData()
  }

  onAdd = () => {
    this.props.history.push('/index/templateEdit')
  }

  // 编辑不要了
  // onEdit = (value, index) => {
  //   this.props.passEditData(value)
  //   this.props.history.push('/index/templateEdit')
  // }

  onDelete = (value, index) => {
    const _this = this
    confirm({
      title: '提示',
      content: '确定删除此条记录',
      maskClosable: true,
      onOk() {
        _this.props.deleteTemplate(value)
      }
    })
  }
  
  onChangePagination = (value) => {
    this.props.setPageNum(value)
    this.props.getData()
  }
  
} 

const mapStateToProps = ({ templateReducer, enumReducer:{ templateStatusEnum } }) => ({ ...templateReducer, templateStatusEnum })

const mapDispatchToProps = { 
  ...actions,
  passEditData: setData,
  getTemplateStatusEnum,
}

export default connect(mapStateToProps, mapDispatchToProps)(Template)